<title>系统用户</title>
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>系统管理</cite></a>
    <a><cite>系统用户</cite></a>
  </div>
</div>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-row">
      <div class="layui-col-xs10">
        <div class="grid-demo grid-demo-bg1">
          <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="app-forum-list">
            <div class="layui-form-item">
              <div class="layui-inline">
                <label class="layui-form-label">账号</label>
                <div class="layui-input-block">
                  <input type="text" name="q" placeholder="请输入" autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-inline">
                <button class="layui-btn layuiadmin-btn-forum-list" lay-submit lay-filter="LAY-app-forumlist-search">
                  <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-forum-list" lay-href="admins/adminform">添加</button>
      </div>
      <table id="LAY-app-forum-list" lay-filter="LAY-app-forum-list"></table>
      <script type="text/html" id="data-control">
        <span class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</span>
        {{#  if(d.status ==1){ }}
        <a class="layui-btn layui-btn-xs layui-btn-warm"  lay-event="status">禁用</a>
        {{#  } }}
        {{#  if(d.status ==0){ }}
        <a class="layui-btn  layui-btn-xs" lay-event="status">恢复</a>
        {{#  } }}
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
      </script>
    </div>
  </div>
</div>

<script>
  layui.use(['table'], function () {
    var $ = layui.$
      , form = layui.form
      , admin = layui.admin
      , setter = layui.setter
        ,view = layui.view
      , table = layui.table;

    //监听搜索
    form.on('submit(LAY-app-forumlist-search)', function (data) {
      var field = data.field;

      //执行重载
      table.reload('LAY-app-forum-list', {
        page: {
          curr: 1
        },
        where: field
      });
    });

    table.render({
      elem: '#LAY-app-forum-list'
      , url: layui.setter.api + 'admins'  //模拟接口
      , where: {
        'access_token': layui.data(setter.tableName, {
          key: setter.request.tokenName
        }), 'pid': layui.router().search.id
      }
      , cols: [[
        { field: 'admin_id', title: 'ID', sort: true, width: 80 }
        , { field: 'admin_name', title: '帐号' }
        , { field: 'group_name', title: '管理组' }
        , { field: 'regtime', title: '注册时间' }
        , { field: 'logtime', title: '最后登录' }
        , { field: 'status_name', title: '帐号状态' }
        , { title: '操作', align: 'center', fixed: 'right', toolbar: '#data-control', width: 180 }
      ]]
      , page: true
      , limit: 20
      , limits: [20, 40, 60, 80, 100]
      , text: '对不起，加载出现异常！'
    });

    table.on('tool(LAY-app-forum-list)', function (obj) {
      var data = obj.data;
      if (obj.event === 'del') {
        layer.confirm('确定删除此条数据？', function (index) {
          admin.req({
            url: layui.setter.api + 'admins'
            , data: { 'id': data.admin_id, 'val': -1, 'opt': 'status' }
            , method: 'post'
            , success: function (data) {
              if (data.code == 0) {
                obj.del();
              }
            }
          });

          layer.close(index);
        });
      } else if (obj.event === 'edit') {
          admin.popup({
              title: '编辑用户'
              ,area: ['550px', '450px']
              ,id: 'LAY-popup-forum-edit'
              ,resize: false
              ,success: function(layero, index){
                  view(this.id).render('admins/adminsedit', data).done(function(){
                      form.render(null, 'layuiadmin-form-list');

                      //监听提交
                      form.on('submit(layuiadmin-app-form-submit)', function(data){
                          var field = data.field; //获取提交的字段

                          //提交 Ajax 成功后，关闭当前弹层并重载表格
                          //提交修改
                          admin.req({
                              url: layui.setter.api+'admins'
                              ,data: data.field
                              ,method:'post'
                              ,success: function(data){
                                  if(data.code == 0){
                                      layui.table.reload('LAY-app-forum-list'); //重载表格
                                      layer.close(index); //执行关闭
                                  }else{
                                      layer.msg(data.msg);
                                  }
                              }
                          });
                      });
                  });
              }
          });
      } else if (obj.event === 'status') {
        layer.confirm('确定修改状态？', function (index) {
          console.log(data.status)
          admin.req({
            url: layui.setter.api + 'admins'
            , data: { 'id': data.admin_id, 'val': data.status == 1 ? "0" : "1", 'opt': 'status' }
            , method: 'post'
            , success: function (data) {
              if (data.code == 0) {
                layui.table.reload('LAY-app-forum-list'); //重载表格
              }
            }
          });

          layer.close(index);
        });
      }
    });
  });

</script>